<?php

namespace App\Http\Controllers\Mini;

use App\Models\Areas;
use App\Models\Articles;
use App\Models\Collections;
use App\Models\Footprints;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    /**
     * 新闻列表
     * @route api.news.newsLists
     * @param page 页码
     * @param limit 条数
     * @param sid 排序 1发布时间 2浏览量 3收藏量
     */
    public function newsLists(Request $request){
        try{
            $page = (int)$request->post('page',1);//页码
            $limit = (int)$request->post('limit',5);//条数
            if (empty($page) || empty($limit)) return response()->json(["code"=>0, "msg"=>'参数错误']);
            $sid = (int)$request->post('sid');//排序 1发布时间 2浏览量 3收藏量
            if ($sid == 1){
                $sort = 'created_at';
            }elseif ($sid == 2){
                $sort = 'view';
            }elseif ($sid == 3){
                $sort = 'collect';
            }else{
                $sort = 'created_at';
            }
            $fields = ['id','thumb','title','view','collect','created_at'];
            $lists = Articles::allLists([], $fields, $page, $limit, $sort, 'desc');
            foreach ($lists as $key=>$val){
                $lists[$key]['view'] = strlen($val['view'])>=4 ? '999+' : $val['view'];
                $lists[$key]['collect'] = strlen($val['collect'])>=4 ? '999+' : $val['collect'];
                $lists[$key]['created_at'] = trim(@date('Y-m-d', strtotime($val['created_at'])));
            }

            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 新闻详情
     * @route api.news.newsDetails
     * @param user_id 用户id
     * @param news_id 新闻id
     * @return is_collect 0:未收藏 1:已收藏
     */
    public function newsDetails(Request $request){
        try{
            $user_id = (int)$request->post('user_id');
            $user_info = Users::where('id',$user_id)->where('status','<>',-1)->first();
            $news_id = (int)$request->post('news_id');//新闻id
            if (empty($news_id)) return response()->json(["code"=>0, "msg"=>'参数错误']);

            $fields = ['id','thumb','title','content','source','created_at','view'];
            $lists = Articles::details(['id'=>$news_id], $fields);
            $lists['created_at'] = trim(@date('Y-m-d', strtotime($lists['created_at'])));

            //判断是否收藏和报名
            $lists['is_collect'] = 0;//默认未收藏
            if (!empty($user_info)){
                if ($user_info['type'] == 1){
                    $collect = Collections::where(['collect_id'=>$news_id,'user_id'=>$user_info['id'],'type'=>2])->value('collect_id');
                    $lists['is_collect'] = empty($collect) ? 0 : 1;
                }
            }
            Articles::where('id',$news_id)->increment('view');//浏览量加1

            //获取下一条
            $next_id = Articles::where('id', '>', $lists['id'])->min('id');
            $lists['next_info'] = Articles::where('id',$next_id)->first(['id','title','thumb','view','created_at']);

            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 收藏新闻
     * @route api.news.newsCollects
     * @param user_id 用户id
     * @param collect_id 收藏内容id
     */
    public function newsCollects(Request $request){
        try{
            $user_id = (int)$request->post('user_id');
            if (empty($user_id)) return response()->json(["code"=>0, "msg"=>'请先登录']);
            $userinfo = Users::where(['id'=>$user_id])->where('status','<>',-1)->first(['type','status']);
            if (empty($userinfo)){
                return response()->json(["code"=>0, "msg"=>'用户不存在,请联系客服!']);
            }else{
                if($userinfo['status'] == 2){
                    return response()->json(["code"=>0, "msg"=>'账户正在审核中，请等待审核!']);
                }elseif($userinfo['status'] == 3){
                    return response()->json(["code"=>0, "msg"=>'账号未通过审核，请前往个人中心查看原因并重新提交审核!']);
                }elseif ($userinfo['status'] == 4){
                    return response()->json(["code"=>0, "msg"=>'账号已锁定，请前往个人中心查看原因或联系客服!']);
                }
                if ($userinfo['type'] != 1) return response()->json(["code"=>0, "msg"=>'供应商不能收藏新闻!']);
            }

            $collect_id = (int)$request->post('collect_id');
            if (empty($collect_id)) return response()->json(["code"=>0, "msg"=>'参数错误']);

            $collect = Collections::where(['collect_id'=>$collect_id, 'user_id'=>$user_id, 'type'=>2])->value('id');
            if (empty($collect)){
                $params = [
                    'collect_id' => $collect_id,
                    'user_id' => $user_id,
                    'collect_at' => date('Y-m-d H:i:s'),
                    'type' => 2,
                ];
                Collections::create($params);
                Articles::where('id',$collect_id)->increment('collect');
                return response()->json(["code"=>1, "msg"=>'收藏成功']);
            }else {
                Collections::where(['collect_id'=>$collect_id, 'user_id'=>$user_id, 'type'=>2])->delete();
                return response()->json(["code"=>1, "msg"=>'取消收藏成功']);
            }
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

}